<?php

namespace App\Handle;
use GuzzleHttp\Client as GuzzleClient;

class Message{
    public static function sendVcode($mobile = '', $code = '', $type = 'vcode', $sign = '便利签'){
        $account = 'lxyzm';
        $pass = 'a1234567';
        $timestamp = date('YmdHis');
        $content = '';
        if($type ==  'vcode'){
            $content = "您的验证码是：{$code}，请于5分钟内输入验证，请勿向他人泄露。";
        }elseif($type ==  'passms'){
            $content = "您已成功注册，您的初始登录密码为：{$code}请勿泄露密码给他人。";
        }elseif($type ==  'resign'){
            $content = "您的客户：{$code}请求再次签约。";
        }

        $params = [
            'form_params' => [
                'action' => 'send',
                'userid' => 1774,
                'timestamp' => $timestamp,
                'sign' => md5($account.$pass.$timestamp),
                'mobile' => $mobile,
                'content' => $content,
                'sendTime' => '',
                'extno' => '',
            ],
            'headers' => [
                'Content-Type' => 'application/x-www-form-urlencoded'
            ]
        ];

        $send_url = 'http://120.76.228.10:8088/v2sms.aspx';
        $res = self::guzzle_post($send_url, $params);
        $result = $res->getBody()->getContents();
        if(stripos($result, 'Success') === false){
            return ['code' => 1, 'text' => '发送失败'];
        }
        return ['code' => 0, 'text' => '发送成功'];
    }


    //发送请求
    public static function guzzle($url, $params)
    {
        $client = new GuzzleClient([
            // 超时
            'timeout' => 60.0,

        ]);
        return $client->request('GET', $url, $params);
    }

    public static function guzzle_post($url, $params){
        $client = new GuzzleClient([
            // 超时
            'timeout' => 60.0,
        ]);
        return $client->request('POST', $url, $params);
    }
}